<?php /* D:\phpStudy\PHPTutorial\WWW\laravel-admin\resources\views/admin/goods/sku.blade.php */ ?>
<div id="app">
    <section class="content-header">
        <h1>
            <small>商品销售属性</small>
        </h1>

    </section>

    <section class="content">
        <div class="row">
            <div class="col-md-12">
                <div class="box box-info">
                    <div class="box-header with-border">

                        <div class="box-tools">
                            <div class="btn-group pull-right" style="margin-right: 5px">
                                <a style="margin:0px 5px;" href="/admin/goods/goodsEdit?goods_id=<?php echo e($goods['goods_id'], false); ?>" class="btn btn-sm btn-default" title="商品编辑"> <span class="hidden-xs">商品编辑</span></a>
                                <a style="margin:0px 5px;" href="/admin/goods/goodsAttr?goods_id=<?php echo e($goods['goods_id'], false); ?>" class="btn btn-sm btn-default" title="规格"> <span class="hidden-xs">规格</span></a>
                                <a style="margin:0px 5px;" href="/admin/goods/goodsAddImg?goods_id=<?php echo e($goods['goods_id'], false); ?>" class="btn btn-sm btn-default" title="图片"> <span class="hidden-xs">图片</span></a>
                                <a style="margin:0px 5px;" href="/admin/goods/goodsSku?goods_id=<?php echo e($goods['goods_id'], false); ?>" class="btn btn-sm btn-default" title="设置商品sku"> <span class="hidden-xs">设置商品sku</span></a>
                                <a style="margin:0px 5px;" href="/admin/goods/goodsLease?goods_id=<?php echo e($goods['goods_id'], false); ?>}" class="btn btn-sm btn-default" title="租期"> <span class="hidden-xs">租期</span></a>
                            </div>
                        </div>



  <span>
    <a class="btn btn-sm btn-primary grid-refresh" title="刷新"><i class="fa fa-refresh"></i><span class="hidden-xs"> 刷新</span></a>
    <a class="btn btn-sm btn-primary" onclick="javascript:history.go(-1);" title="返回"> <span class="hidden-xs"> 返回</span></a>
  </span>
                    </div>

                    <div class="box-body table-responsive no-padding">
                        <table class="table table-hover">
                            <tr>
                                <td colspan="3"><h3>商品名称：<?php echo e($goods['goods_name'], false); ?></h3></td>
                            </tr>
                            <?php if($sku): ?>
                                <?php $__currentLoopData = $sku; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $v): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                    <tr  class="attr_type<?php echo e($v['attr_id'], false); ?>">
                                        <td colspan="2"  >
                                            属性名：<input type="text" id="attr_type<?php echo e($v['attr_id'], false); ?>" style="height:30px;border:1px solid #adadad;width:400px;padding-left:5px;" value="<?php echo e($v['attr_type'], false); ?>">
                                            <button style="background: #0d6aad;color:#f2fbff;border:0px;height:30px;" data-id="<?php echo $v['attr_id']; ?>" onclick="edit_attr_type(this)" type="button">修改</button>
                                        </td>
                                        <td>
                                            <button style="background: #adadad;border:0px;padding:5px;" data-id="<?php echo $v['attr_id']; ?>" onclick="del_attr_type(this)" type="button">删除</button>
                                        </td>
                                    </tr>
                                    <?php if($v['zhi']): ?>
                                        <?php $__currentLoopData = $v['zhi']; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $vv): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                            <tr class="attr_type<?php echo e($v['attr_id'], false); ?>  attr_val<?php echo e($vv['id'], false); ?>">
                                                <td style="text-align: center;">
                                                    <div>
                                                      属性值：  <input type="text" id="attr_val<?php echo e($vv['id'], false); ?>" value="<?php echo e($vv['attr_name'], false); ?>" style="height:30px;border:1px solid #a1cbef;width:400px;padding-left:5px;">
                                                        <button style="background: #0d6aad;color:#f2fbff;border:0px;height:30px;"  data-attr-id="<?php echo e($v['attr_id'], false); ?>" data-id="<?php echo e($vv['id'], false); ?>" onclick="edit_attr_val(this)"   >编辑</button>
                                                    </div>
                                                </td>
                                                <td>
                                                    <img class="attr_img<?php echo e($vv['id'], false); ?>" data-action="zoom" width="100" src="<?php echo e($url, false); ?><?php echo e($vv['attr_img'], false); ?>" />
                                                    <button style="background: #0d6aad;color:#f2fbff;border:0px;height:30px;" data-id="<?php echo e($vv['id'], false); ?>" onclick="$('#imgfile').attr('data-id',$(this).attr('data-id'));$('#imgfile').click();" type="button">更新图片</button>
                                                    <button style="background: #1a2226;color:#f2fbff;border:0px;height:30px;" data-id="<?php echo e($vv['id'], false); ?>" onclick="del_img(this)" type="button">删除图片</button>
                                                </td>
                                                <td>
                                                    <button data-id="<?php echo e($vv['id'], false); ?>" onclick="del_attr_val(this)" style="background: #a6e1ec;border:0px;padding:5px;" type="button">删除</button>
                                                </td>
                                            </tr>
                                        <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                                    <?php endif; ?>

                                <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                            <?php endif; ?>

                        </table>
                    </div>

                    <input type="file" data-id="0" onchange="upimg(this)" name="img" id="imgfile" style="display:none;">

                    <!-- /.box-header -->
                    <!-- form start -->

                    <h3  >添加属性</h3>
                    <form id="addForm"   >

                        <table style="width:50%; border:1px solid #abcdef">
                            <tr>
                                <td style="padding-left:5px;">属性名：</td>
                                <td style="padding:5px 5px;">
                                    <select name="id">
                                        <option value="0">请选择</option>
                                        <?php if($sku): ?>
                                            <?php $__currentLoopData = $sku; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $v): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                                <option value="<?php echo e($v['attr_id'], false); ?>"><?php echo e($v['attr_type'], false); ?></option>
                                            <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                                        <?php endif; ?>
                                    </select>
                                    <input type="text" name="newattr" placeholder="请输入新的属性名" style="height:30px;margin-left:10px;padding-left:5px;border:1px solid #a1cbef;width:300px;">
                                </td>
                            </tr>
                            <tr>
                                <td style="height:30px;padding-left:5px;">属性值：</td>
                                <td style="padding-left:5px;">
                                    <a type="button" onclick="add_zhi(this)">【+】</a>
                                    <div style="padding:5px;"> <input style="padding-left:5px;height:30px;width:300px;border:1px solid #a1cbef"  type="text" name="zhi[]" /></div>

                                </td>
                            </tr>
                        </table>

                        <!-- /.box-body -->

                        <div class="box-footer">
                            <?php echo csrf_field(); ?>
                            <input type="hidden" id="goods_id" name="goods_id" value="<?php echo e($goods['goods_id'], false); ?>">
                            <div class="col-md-2">
                            </div>
                            <div class="col-md-8">
                                <div class="btn-group pull-left">
                                    <button type="button" onclick="tijiao()" class="btn btn-primary">添加</button>
                                </div>
                            </div>
                        </div>
                    </form>
                </div>

            </div></div>
    </section>
</div>
<script data-exec-on-popstate>
    var s='';
    $(function(){
        //刷新
        $('.grid-refresh').on('click', function() {
            $.pjax.reload('#pjax-container');
            toastr.success('刷新成功 !');
        });

        $('.imgedit').click(function(e){
            $("#upImg").attr('data-good',$(this).attr('data-good'));
            $("#upImg").attr('data-id',$(this).attr('data-id'));
            $("#upImg").attr('data-type',$(this).attr('data-type'));
            console.log($(this).attr('data-good'));
            $("#upImg").click();
            s=$(this);
        });
        $("input.img").fileinput({
            "overwriteInitial":false,
            "initialPreviewAsData":true,
            "browseLabel":"浏览",
            "showRemove":false,
            "showUpload":false,
            "dropZoneEnabled":false,
            "allowedFileTypes":["image"]});

        $('.imgdel').click(function(e){

            var id=$(this).attr('data-id');
            var goods_id=$(this).attr('data-good');
            var type=$(this).attr('data-type');
            var b=$(this);

            var data=new FormData();
            data.append('id',id);
            data.append('goods_id',goods_id);
            data.append('type',type);
            data.append('_token',LA.token);

            if(confirm('确定要删除吗？')){

                $.ajax({
                    url:'/admin/goods/goodsDeleteImg',
                    data:data,
                    type:'post',
                    dataType:'json',
                    contentType:false,
                    processData:false,
                    success:function (m) {
                        if(m.status==1){
                            toastr.success(m.message);
                            if(id>0){
                                $(b).parent().remove();
                            }else{
                                $(b).parent().find("img").attr('src','');
                            }

                        }else{
                            toastr.error(m.message);
                        }
                    }
                });
            }

        });
    })

    function add_zhi(e){
        $.ajax({
            url:'/admin/goods/skuHtml',
            type:'get',
            dataType:'json',
            success:function (m) {
                $(e).parent().append(m.data.html);
            }
        });
    }

    function del_zhi(e){
        $(e).parent().remove();
    }
    function upimg(e){
        var id=$(e).attr('data-id');
        var goods_id=$(e).attr('data-good');
        var type=$(e).attr('data-type');
        var file=e.files[0];

        var data=new FormData();
        data.append('img',file);
        data.append('id',id);
        data.append('goods_id',goods_id);
        data.append('type',type);
        data.append('action','update');
        data.append('_token',LA.token);
        $.ajax({
            url:'/admin/goods/goodsAddImg',
            data:data,
            type:'post',
            dataType:'json',
            contentType:false,
            processData:false,
            success:function (m) {
                if(m.status==1){
                    toastr.success(m.message);
                    $(s).parent().find("img").attr('src',m.data.url);
                }else{
                    toastr.error(m.message);
                }
            }
        });

    }

    //编辑属性名
    function edit_attr_type(e){
        var id=$(e).attr('data-id');
        var name=$("#attr_type"+id).val();
        var goods_id=$("#goods_id").val();
        if(!name){
            toastr.error('属性名不能为空');
             return false;
        }
        var data=new FormData();
        data.append('id',id);
        data.append('attr_type',name);
        data.append('goods_id',goods_id);
        data.append('_token',LA.token);
        $.ajax({
            url:"/admin/goods/ajaxEditAttrType",
            data:data,
            type:'post',
            dataType:'json',
            contentType:false,
            processData:false,
            success:function (m) {
                if(m.status==1){
                    toastr.success(m.message);
                }else{
                    toastr.error(m.message);
                }
            }
        });
    }
    //删除属性
    function del_attr_type(e){
        var r=confirm("确定要删除吗？");
        if(!r) return false;
        var attr_id=$(e).attr('data-id');
        var data=new FormData();
        data.append('attr_id',attr_id);
        data.append('_token',LA.token);
        $.ajax({
            url:"/admin/goods/ajaxDelAttrType",
            data:data,
            type:'post',
            dataType:'json',
            contentType:false,
            processData:false,
            success:function (m) {
                if(m.status==1){
                    toastr.success(m.message);
                    $(".attr_type"+attr_id).remove();
                }else{
                    toastr.error(m.message);
                }
            }
        });
    }

    //编辑属性值
    function edit_attr_val(e){
        var id=$(e).attr('data-id');
        var attr_id=$(e).attr('data-attr-id');
        var name=$("#attr_val"+id).val();
        var data=new FormData();
        data.append('id',id);
        data.append('attr_name',name);
        data.append('attr_id',attr_id);
        data.append('_token',LA.token);
        $.ajax({
            url:"/admin/goods/ajaxEditAttrVal",
            data:data,
            type:'post',
            dataType:'json',
            contentType:false,
            processData:false,
            success:function (m) {
                if(m.status==1){
                    toastr.success(m.message);
                }else{
                    toastr.error(m.message);
                }
            }
        });
    }

    //删除属性值
    function del_attr_val(e){
        var r=confirm("确定要删除吗？");
        if(!r) return false;
        var id=$(e).attr('data-id');
        var data=new FormData();
        data.append('id',id);
        data.append('_token',LA.token);
        $.ajax({
            url:"/admin/goods/ajaxDelAttrVal",
            data:data,
            type:'post',
            dataType:'json',
            contentType:false,
            processData:false,
            success:function (m) {
                if(m.status==1){
                    toastr.success(m.message);
                    $(".attr_val"+id).remove();
                }else{
                    toastr.error(m.message);
                }
            }
        });
    }

    //更新图片
    function upimg(e){
        var id=$(e).attr('data-id');
        var file=e.files[0];
        var data=new FormData();
        data.append('img',file);
        data.append('id',id);
        data.append('_token',LA.token);
        $.ajax({
            url:'/admin/goods/ajaxUploadAttrImg',
            data:data,
            type:'post',
            dataType:'json',
            contentType:false,
            processData:false,
            success:function (m) {
                if(m.status==1){
                    toastr.success(m.message);
                    $(".attr_img"+id).attr('src',m.data.img);
                }else{
                    toastr.error(m.message);
                }
            }
        });
    }

    //删除属性值图片
    function del_img(e){
        var r=confirm("确定要删除吗？");
        if(!r) return false;
        var id=$(e).attr('data-id');
        var data=new FormData();
        data.append('id',id);
        data.append('_token',LA.token);
        $.ajax({
            url:"/admin/goods/ajaxDelAttrImg",
            data:data,
            type:'post',
            dataType:'json',
            contentType:false,
            processData:false,
            success:function (m) {
                if(m.status==1){
                    toastr.success(m.message);
                    $(".attr_img"+id).attr('src','');
                }else{
                    toastr.error(m.message);
                }
            }
        });
    }

    function tijiao(){
        var data=new FormData($("#addForm")[0]);
        $.ajax({
            url:"/admin/goods/sku",
            type:'post',
            data:data,
            dataType:'json',
            processData: false,
            contentType: false,
            success:function(m){
                if(m.status==1){
                    toastr.success(m.message);
                    window.location.reload();
                }else{
                    toastr.error(m.message);
                }
            }
        });
    }
</script>


<script src="/js/zoom.js"></script>